package com.modules.version.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.response.ResponseVO;
import com.common.utils.R;
import com.modules.version.entity.Version;
import com.modules.version.service.IVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/version")
public class VersionController {

    @Autowired
    private IVersionService versionService;

    // 版本的分页列表
    @RequestMapping("/getPageList")
    public R getPageList(@RequestParam Map<String, Object> params){
        return versionService.getPageList(params);
    }

    // 获得单个版本信息
    @GetMapping ("/getEntityById/{id}")
    public R getEntityById(@PathVariable Integer id){
        Version version = versionService.getOne(new QueryWrapper<Version>().lambda().eq(Version::getId, id));
        return R.ok().put("data", version);
    }

    // 添加/修改 版本
    @PostMapping("/saveEntity")
    public R saveEntity(@RequestBody Version version){
        if (version.getId() == null){
            version.setCreateTime(new Date());
        }

        versionService.saveOrUpdate(version);
        return R.ok();
    }

    // 删除版本
    @DeleteMapping("/removeEntity/{id}")
    public R removeEntity(@PathVariable Integer id){
        Version version = this.versionService.getById(id);
        versionService.removeById(version);
        return R.ok();
    }

}
